package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.system.constant.IpAddressConstant;
import com.ruoyi.system.domain.HUAWEI.EmsBarcodeAttribute;
import com.ruoyi.system.domain.HUAWEI.EmsBarcodeRelation;
import com.ruoyi.system.domain.HUAWEI.EmsBarcodeShipment;
import com.ruoyi.system.domain.MES.OrderInfoUpDTO;
import com.ruoyi.system.domain.WMS.RefuelingOrderDTO;
import com.ruoyi.system.mapper.HUAWEI.EmsBarcodeRelationMapper;
import com.ruoyi.system.mapper.HUAWEI.EmsBarcodeShipmentMapper;
import com.ruoyi.system.service.IEmsBarcodeRelationService;
import com.ruoyi.system.service.IEmsBarcodeShipmentService;
import com.ruoyi.system.utils.InterfaceLogUtil.InterfaceLogUtil;
import com.ruoyi.system.utils.rabbitMq.direct.DirectSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author GE-LV5
 * @create 2023/4/10 13:47
 */
@Service
@Slf4j
public class EmsBarcodeShipmentServiceImpl extends ServiceImpl<EmsBarcodeShipmentMapper, EmsBarcodeShipment> implements IEmsBarcodeShipmentService {

    @Autowired
    private DirectSender directSender;
    @Autowired
    private InterfaceLogUtil interfaceLogUtil;
    @Autowired
    private IEmsBarcodeRelationService iEmsBarcodeRelationService;
    @Autowired
    private EmsBarcodeRelationMapper emsBarcodeRelationMapper;


    @Override
    public String deleteShipment(List<String> list) {

        Date startDate = new Date();

        if (IterUtil.isEmpty(list)) {
            return "出库删除信息参数为空";
        }

        try {
            list.stream().forEach(i -> {
                EmsBarcodeShipment emsBarcodeShipment = new EmsBarcodeShipment();
                emsBarcodeShipment.setLastupdateDate(new Date());
                emsBarcodeShipment.setSegment6(1);//1表示删除
                baseMapper.update(emsBarcodeShipment, new QueryWrapper<EmsBarcodeShipment>().eq("ems_trans_id", i));
            });
        } catch (Exception e) {
            //mes接口异常日志记录
            interfaceLogUtil.insertLog(JSONUtil.toJsonStr(list),
                    startDate, new Date(), InterfaceLogUtil.DIR_MES_MOM,
                    InterfaceLogUtil.TYPE_HTTP,
                    e.getMessage(),
                    InterfaceLogUtil.INTERFACENAME_EXCEPTION_CKSC
            );

        }
        return "删除成功";
    }

    @Override
    public String sendOrderInfoLine01(OrderInfoUpDTO orderInfoUpDTO) {

        Date startDate = new Date();

        String resp = HttpUtil.post(IpAddressConstant.MES_IP01 + "/api/services/app/WorkOrder/ORDER_INFO", JSONUtil.toJsonStr(orderInfoUpDTO));

        interfaceLogUtil.insertLog(JSONUtil.toJsonStr(orderInfoUpDTO),
                startDate, new Date(), InterfaceLogUtil.DIR_MOM_MES,
                InterfaceLogUtil.TYPE_HTTP,
                resp,
                InterfaceLogUtil.INTERFACENAME_FSGDHX
        );

        return resp;
    }

    @Override
    public String sendOrderInfoLine02(OrderInfoUpDTO orderInfoUpDTO) {

        Date startDate = new Date();

        String resp = HttpUtil.post(IpAddressConstant.MES_IP02 + "/api/services/app/WorkOrder/ORDER_INFO", JSONUtil.toJsonStr(orderInfoUpDTO));

        interfaceLogUtil.insertLog(JSONUtil.toJsonStr(orderInfoUpDTO),
                startDate, new Date(), InterfaceLogUtil.DIR_MOM_MES,
                InterfaceLogUtil.TYPE_HTTP,
                resp,
                InterfaceLogUtil.INTERFACENAME_FSGDZX
        );
        return resp;
    }

    @Override
    public String rabbitmqSendTest(String queue) {
        String s = HttpUtil.get("https://openapi-nexus-stg.nio.com");
        return s;
    }

    @Override
    public String materielDemandOrders(List<RefuelingOrderDTO> refuelingOrderDTOs) {

        //接口请求开始时间
        Date startDate = new Date();

        if (IterUtil.isEmpty(refuelingOrderDTOs)) {
            throw new CustomException("请求参数不能为空！");
        }
        String resp = HttpUtil.post(IpAddressConstant.WMS_IP + "/api/app/default-materiel-demand/materiel-demand-orders",
                JSONUtil.toJsonStr(refuelingOrderDTOs));

        //日志记录
        interfaceLogUtil.insertLog(JSONUtil.toJsonStr(refuelingOrderDTOs),
                startDate, new Date(), InterfaceLogUtil.DIR_MOM_WMS,
                InterfaceLogUtil.TYPE_HTTP,
                resp,
                InterfaceLogUtil.INTERFACENAME_CXJL
        );


        Map map = JSONUtil.toBean(resp, Map.class);
        boolean rtnSuccess = (Boolean) map.get("rtnSuccess");
        if (!rtnSuccess) {
            return "请求失败，失败信息【" + (String) map.get("rtnMsg") + "】";
        }
        return "请求成功";
    }

    @Override
    public String unbindingRelation(List<EmsBarcodeRelation> list) {
        list.stream().forEach(i -> {
            List<EmsBarcodeRelation> emsBarcodeRelations = emsBarcodeRelationMapper.selectList(new QueryWrapper<EmsBarcodeRelation>()
                    .eq("bar_code", i.getBarCode())
                    .eq("son_bar_code", i.getSonBarCode())
                    .isNull("segment5")
            );

            if(IterUtil.isNotEmpty(emsBarcodeRelations)){
                iEmsBarcodeRelationService.unbindingRelation(emsBarcodeRelations,i.getUpdatedDate());
            }

        });
        return "解绑处理中,请移步MOM系统查看结果";
    }

    @Override
    public List<EmsBarcodeRelation> queryRelationUnbind(EmsBarcodeRelation emsBarcodeRelation) {

        QueryWrapper<EmsBarcodeRelation> lqw = new QueryWrapper<EmsBarcodeRelation>();
        if(StrUtil.isNotEmpty(emsBarcodeRelation.getBarCode())){
        lqw.eq("bar_code", emsBarcodeRelation.getBarCode());
        }

        if(StrUtil.isNotEmpty(emsBarcodeRelation.getSonBarCode())){
            lqw.eq("son_bar_code", emsBarcodeRelation.getSonBarCode());
        }

        lqw.eq("segment5", 1);

        if(emsBarcodeRelation.getUpdatedDate()!=null){
            lqw.last("AND CONVERT(VARCHAR, updated_date, 120) = " + "'" + DateUtil.format(emsBarcodeRelation.getUpdatedDate(), "yyyy-MM-dd HH:mm:ss") + "'");
        }

        List<EmsBarcodeRelation> barcodeRelation = emsBarcodeRelationMapper.selectList(lqw);

        return barcodeRelation;
    }

    @Override
    public List<EmsBarcodeRelation> queryRelationBind(EmsBarcodeRelation emsBarcodeRelation) {
        QueryWrapper<EmsBarcodeRelation> lqw = new QueryWrapper<EmsBarcodeRelation>();
        if(StrUtil.isNotEmpty(emsBarcodeRelation.getBarCode())){
            lqw.eq("bar_code", emsBarcodeRelation.getBarCode());
        }

        if(StrUtil.isNotEmpty(emsBarcodeRelation.getSonBarCode())){
            lqw.eq("son_bar_code", emsBarcodeRelation.getSonBarCode());
        }

        lqw.isNull("segment5");

        if(emsBarcodeRelation.getUpdatedDate()!=null){
            lqw.last("AND CONVERT(VARCHAR, updated_date, 120) = " + "'" + DateUtil.format(emsBarcodeRelation.getUpdatedDate(), "yyyy-MM-dd HH:mm:ss") + "'");
        }

        List<EmsBarcodeRelation> barcodeRelations = emsBarcodeRelationMapper.selectList(lqw);

        return barcodeRelations;
    }


}
